using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace GameTank
{
    public class MapDTO
    {
        string nextMapPath = "~vitory";

        public string NextMapPath
        {
            get { return nextMapPath; }
            set { nextMapPath = value; }
        }

        List<string> checkAlive;

        public List<string> CheckAlive
        {
            get { return checkAlive; }
            set { checkAlive = value; }
        }
        List<string> checkDead;

        public List<string> CheckDead
        {
            get { return checkDead; }
            set { checkDead = value; }
        }

        public MapDTO()
        {
            characterDTOs = new List<CharacterDTO>();
            constructionsDTO = new List<ConstructionDTO>();
            checkAlive = new List<string>();
            checkDead = new List<string>();
        }
        List<CharacterDTO> characterDTOs;

        public List<CharacterDTO> CharacterDTOs
        {
            get { return characterDTOs; }
            set { characterDTOs = value; }
        }
        List<ConstructionDTO> constructionsDTO;

        public List<ConstructionDTO> ConstructionsDTOs
        {
            get { return constructionsDTO; }
            set { constructionsDTO = value; }
        }

        public XElement ToXml()
        {
            XElement element = new XElement("mapDTO");
            element.Add(ToXml(this.CharacterDTOs));
            element.Add(ToXml(this.ConstructionsDTOs));
            element.Add(ToXml(this.checkAlive,"listCheckAlive"));
            element.Add(ToXml(this.checkAlive,"listCheckDead"));
            element.SetAttributeValue("nextMapPath", this.NextMapPath);
            return element;
        }



        private XElement ToXml(List<ConstructionDTO> constructions)
        {
            XElement element = new XElement("listConstruction");
            element.SetAttributeValue("value", constructions.Count);

            for (int i = 0; i < constructions.Count; ++i)
            {
                element.Add(constructions[i].ToXml());
            }
            return element;
        }

        private XElement ToXml(List<CharacterDTO> characterDTOs)
        {
            XElement element = new XElement("listCharacter");
            element.SetAttributeValue("value", characterDTOs.Count);

            for (int i = 0; i < characterDTOs.Count; ++i)
            {
                element.Add(characterDTOs[i].ToXml());
            }
            return element;
        }

        private XElement ToXml(List<string> checkAlive, string abc)
        {
            XElement element = new XElement(abc);
            element.SetAttributeValue("value",checkAlive.Count );

            for (int i = 0; i < checkAlive.Count; ++i)
            {
                element.Add(ToXml(checkAlive[i]));
            }
            return element;
        }

        XElement ToXml(string name)
        {
            XElement a= new XElement("Name");
            a.SetAttributeValue("value",name);
            return a;
        }


        public static MapDTO Parse(XElement element)
        {
            MapDTO map = new MapDTO();

            map.NextMapPath = element.Attribute("nextMapPath").Value;
            map.CharacterDTOs = ReadListCharacter(element.Element("listCharacter"));
            map.ConstructionsDTOs = ReadistListConstruction(element.Element("listConstruction"));
            map.checkAlive = ReadistListCheckAlive(element.Element("listCheckAlive"));
            map.checkAlive = ReadistListCheckDead(element.Element("listCheckDead"));
            return map;
        }

        public static List<string> ReadistListCheckAlive(XElement element)
        {
            List<string> kq = new List<string>();
            var Elements = element.Descendants("Name");
            foreach (var XEle in Elements)
            {
                kq.Add(XEle.Attribute("value").Value);
            }
            return kq;
        }

        public static List<string> ReadistListCheckDead(XElement element)
        {
            List<string> kq = new List<string>();
            var Elements = element.Descendants("Name");
            foreach (var XEle in Elements)
            {
                kq.Add(XEle.Attribute("value").Value);
            }
            return kq;
        }
        public static List<CharacterDTO> ReadListCharacter(XElement element)
        {
            List<CharacterDTO> kq = new List<CharacterDTO>();
            var Elements = element.Descendants("Character");
            foreach (var XEle in Elements)            
            {
                kq.Add(CharacterDTO.Parse(XEle));
            }
            return kq;
        }

        public static List<ConstructionDTO> ReadistListConstruction(XElement element)
        {
            List<ConstructionDTO> kq = new List<ConstructionDTO>();
            var Elements = element.Descendants("Construction");
            foreach (var XEle in Elements)
            {
                kq.Add(ConstructionDTO.Parse(XEle));
            }
            return kq;
        }      

        public void Save(string fileName)
        {
            XDocument doc = new XDocument();
            doc.Add(ToXml());
            doc.Save(fileName);
        }

        public static MapDTO Load(string filenPath)
        {
            XDocument doc =  XDocument.Load(filenPath);
            MapDTO kq = MapDTO.Parse(doc.Root);
            return kq;
        }
    }
}
